/*This hashing program uses a single hash table with size size equal to number of records and so alpha is 1.
The function is ((k mod m)+i)mod m     where m is size of table and k is sum of ascii values of the characters*/

#include<string.h>
#include<stdio.h>
#include<stdlib.h>

struct node{
char user[33];
char pass[33];
};

int ctr=0,chk=0,flag1=0;

struct table{
struct node *next;
};


int hash(int sum,struct table tab[])
{	int loc;
        loc=sum%ctr;
	while(1)
        {   if(tab[loc].next==NULL)
   	       return loc;
	    else
	    {   loc+=1;
                loc%=ctr;
            }
        }//while
}//hash

int search(int asciisum,char t[33],struct table tab[])
{   int loc=asciisum%ctr,i;
    for(i=loc;i<ctr;i++)
       if(!strcmp(t,tab[i].next->user))
          return i;
    for(i=0;i<loc;i++)
       if(!strcmp(t,tab[i].next->user))
          return i;
    printf("\nNo such userid\n");
    return -1;
}//search
         
void insert(char tempu[33],char temp[33],int loc,struct table tab[])
{   struct node *new=malloc(sizeof(struct node));
    strcpy(new->user,tempu);
    strcpy(new->pass,temp);
    tab[loc].next=new;
}//insert


void main()
{   FILE *f;
    char c[2];
    c[1]='\0';
    int asciisum=0,loc,i,j;
    f=fopen("user.txt","r");
    c[0]=getc(f);
    while(c[0]!=EOF)
    {	if(c[0]=='\n')
           ctr++;
        c[0]=getc(f);
    }//while
    fclose(f);
    f=fopen("user.txt","r");
    struct table tab[ctr];
    for(i=0;i<ctr;i++)
       tab[i].next=NULL;
    for(i=0;i<ctr;i++)
    {   char tempu[33]="\0";
        c[0]=getc(f);
        while(c[0]!=EOF)
    	{  j=c[0];
       	   asciisum+=j;
           if(c[0]==' ')
              break;
           else
              strcat(tempu,c);
           c[0]=getc(f);
        }//while
     	char temp[33]="\0";
     	c[0]=getc(f);
     	while(c[0]!=EOF)
     	{  if(c[0]=='\n')
              break;
           else
              strcat(temp,c);
           c[0]=getc(f);
        }//while  
        loc=hash(asciisum,tab);
        insert(tempu,temp,loc,tab);
    }//for
    fclose(f);
    do
    {   char temp[33];
        printf("\n\tEnter username(enter 0 to exit program):");
        scanf("%s",temp);
        if(!strcmp(temp,"0"))
        {   flag1=1;
            continue;
        }
        else
        {   asciisum=0;
            while(temp[i]!='\0')
            {   j=temp[i];
                asciisum+=j;
                i++;
            }//while
            i=search(asciisum,temp,tab);
            if(i==-1)
              continue;
            int flag=0;
            do
            {   printf("\n\n\tEnter password:");
                scanf("%s",temp);      
                if(!strcmp(temp,tab[i].next->pass))
                {   printf("\nUsername and password match\n");
                    flag=1;
                }
                else
                {   printf("\nIncorrect password\n"); 
                    chk+=1;
                    if(chk==3)
                    {   flag=1;
                        chk=0;
                        printf("Too many incorrect attempts,access is forbidden\n");
                    }
                }
            }while(flag!=1);
        }//else
     }while(flag1!=1); 
}//main
